/*
 * Copyright 2013 HVTTGROUP
 * Tool Genneric code by hvttgroup
 * @version v1.0
 * @see http://hvttgroup.com
 *
 */
package com.hvtt.web.models;

import com.hvtt.web.connection.Command;
import com.hvtt.web.connection.WEBConnection;
import com.hvtt.web.connection.WEBParamCollection;
import java.lang.reflect.Field;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

/**
 *
 * @author Nguyen Hoang Danh
 */
public class Contact implements Serializable {

// <editor-fold defaultstate="collapsed" desc="Generic code properties for class">
    private String contactID = "";
    private String fullName = "";
    private String companyName = "";
    private String email = "";
    private String phone = "";
    private String address = "";
    private String content = "";
    private Date dateContact = Calendar.getInstance().getTime();
    private boolean xem = false;
    private WEBConnection webConnection;
    private static String STORE_NAME = "{call proc_contact(?,?,?,?,?,?,?,?,?,?)}";
    private static String TABLE_NAME = "contact";
    private static final String key = "ContactID";
//</editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code Constructor class">
    public Contact() {
        webConnection = WEBConnection.getInstance();
    }

    public Contact(String contactID, String fullName, String companyName, String email, String phone, String address, String content, Date dateContact, boolean xem) {
        this.contactID = contactID;
        this.fullName = fullName;
        this.companyName = companyName;
        this.email = email;
        this.phone = phone;
        this.address = address;
        this.content = content;
        this.dateContact = dateContact;
        this.xem = xem;
        webConnection = WEBConnection.getInstance();
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code init param">
    private WEBParamCollection fillParamsAndAction(String action) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(this.getContactID());
        params.add(this.getFullName());
        params.add(this.getCompanyName());
        params.add(this.getEmail());
        params.add(this.getPhone());
        params.add(this.getAddress());
        params.add(this.getContent());
        params.add(this.getDateContact());
        params.add(this.getXem());
        return params;
    }

    private WEBParamCollection fillParamsAndAction(String action, Contact contact1) {
        WEBParamCollection params = WEBParamCollection.getInstance();
        params.add(action);
        params.add(contact1.getContactID());
        params.add(contact1.getFullName());
        params.add(contact1.getCompanyName());
        params.add(contact1.getEmail());
        params.add(contact1.getPhone());
        params.add(contact1.getAddress());
        params.add(contact1.getContent());
        params.add(contact1.getDateContact());
        params.add(contact1.getXem());
        return params;
    }

// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code event">
    public boolean insert(Contact varContact) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add", varContact));
        return bExcute;
    }

    public boolean save() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Add"));
        return bExcute;
    }

    public boolean update(Contact varContact) {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update", varContact));
        return bExcute;
    }

    public boolean update() {
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Update"));
        return bExcute;
    }

    public boolean remove(String key) {
        this.setContactID(key);
        boolean bExcute = webConnection.executeUpdate(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("Delete"));
        return bExcute;
    }

    public boolean remove(List<Contact> listContact) {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean result = false;
        String sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
        for (int i = 0; i < listContact.size(); i++) {
            sql = sql + "?,";
            p.add(listContact.get(i).getContactID());
            if (i % 900 == 0) {
                sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
                result = webConnection.executeUpdate(sql, Command.sqlText, p);
                p.clear();
                sql = "delete from " + TABLE_NAME + " where " + TABLE_NAME + "." + key + " in (";
            }
        }
        if (sql.lastIndexOf(",") != -1) {
            sql = sql.substring(0, sql.lastIndexOf(",")) + ")";
            result = webConnection.executeUpdate(sql, Command.sqlText, p);
        }
        return result;
    }

    public boolean removeAll() {
        WEBParamCollection p = WEBParamCollection.getInstance();
        boolean bExcute = this.getWebConnection().executeUpdate("delete from " + TABLE_NAME, Command.sqlText, p);
        return bExcute;
    }

    public Contact findByKey(String key) {
        Contact varContact1 = null;
        try {
            this.setContactID(key);
            List<Contact> result = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetByKey"), Contact.class);
            if (!result.isEmpty()) {
                varContact1 = result.get(0);
            }
        } catch (SQLException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return varContact1;
    }

    public List<Contact> findAll() {
        List<Contact> lstContact = null;
        try {
            lstContact = webConnection.executeQuery(STORE_NAME, Command.sqlStoredProcedure, fillParamsAndAction("GetAll"), Contact.class);
        } catch (SQLException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
        return lstContact == null ? new ArrayList<Contact>() : lstContact;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="Generic code getter and setter for properties of class">
    public String getContactID() {
        return contactID;
    }

    public String getFullName() {
        return fullName;
    }

    public String getCompanyName() {
        return companyName;
    }

    public String getEmail() {
        return email;
    }

    public String getPhone() {
        return phone;
    }

    public String getAddress() {
        return address;
    }

    public String getContent() {
        return content;
    }

    public Date getDateContact() {
        return dateContact;
    }

    public void setDateContact(Date dateContact) {
        this.dateContact = dateContact;
    }

    public boolean getXem() {
        return xem;
    }

    public void setContactID(String contactID) {
        this.contactID = contactID;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setXem(boolean xem) {
        this.xem = xem;
    }

    public void resetAttribute() {
        contactID = "";
        fullName = "";
        companyName = "";
        email = "";
        phone = "";
        address = "";
        content = "";
        xem = false;
    }

    public void setWebConnection(WEBConnection webConnection) {
        this.webConnection = webConnection;
    }

    public WEBConnection getWebConnection() {if (webConnection == null) {webConnection = new WEBConnection();}return webConnection;}

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code hasCode and equals method">
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.contactID).append(this.fullName).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj instanceof Contact) {
            Contact otherObj = (Contact) obj;
            return new EqualsBuilder().append(this.contactID, otherObj.contactID).append(this.fullName, otherObj.fullName).isEquals();
        }
        return false;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code filterFields">
    public static Field[] filterFields() {
        try {
            Class c = Contact.class;
            return new Field[]{c.getDeclaredField("contactID"), c.getDeclaredField("fullName"), c.getDeclaredField("companyName"), c.getDeclaredField("email"), c.getDeclaredField("phone"), c.getDeclaredField("address"), c.getDeclaredField("content")};
        } catch (NoSuchFieldException | SecurityException ex) {
            Logger.getLogger(Contact.class.getName()).log(Level.SEVERE, null, ex);
        }
        return new Field[]{};
    }
    
    public String validate(){
        if(StringUtils.isBlank(this.getContactID())){
            return "MSG_ERROR_VALIDATE";
        }
        return null;
    }

//</editor-fold>
// <editor-fold defaultstate="collapsed" desc="Generic code other method">
    public Contact newInstance() {
        try {
            return this.getClass().newInstance();
        } catch (InstantiationException | IllegalAccessException ex) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
        }
        return new Contact();
    }

    /**
     * Copy properties form Contact to this
     *
     * @param contact1
     * @return Contact
     */
    public Contact newInstance(Contact contact1) {
        this.setContactID(contact1.getContactID());
        this.setFullName(contact1.getFullName());
        this.setCompanyName(contact1.getCompanyName());
        this.setEmail(contact1.getEmail());
        this.setPhone(contact1.getPhone());
        this.setAddress(contact1.getAddress());
        this.setContent(contact1.getContent());
        this.setDateContact(contact1.getDateContact());
        this.setXem(contact1.getXem());
        return this;
    }
//</editor-fold>
}
